package com.ruoyi.web.controller.system.api.cc;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.framework.interceptor.Authentication;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.FseLocation;
import com.ruoyi.system.api.domain.Login;
import com.ruoyi.system.api.domain.User;
import com.ruoyi.system.api.domain.dto.FseLocationDto;
import com.ruoyi.system.api.service.LocationService;
import com.ruoyi.system.api.service.LoginService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;

/**
 * @ClassName LocationController
 * @Description: 用户实时位置查询
 * @Author shihx
 * @Date 2020/3/2
 * @Version V1.0
 **/
@Api(tags="FSE用户实时位置查询")
@Controller
@RequestMapping("/api/cc/location")
public class LocationController extends BaseController {

    @Autowired
    private LocationService locationService;

    private static final Logger log = LoggerFactory.getLogger(LocationController.class);

    /**
     * 获取TL下所有FSE的实时位置（经纬度）接口
     * @methodName: getFseLocation
     * @description: TODO
     * @param TeamleaderId
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020/3/2 9:32
    **/
    @GetMapping("/getFseLocation")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "获取TL下所有FSE的实时位置（经纬度）接口", httpMethod = "GET", notes = "获取TL下所有FSE的实时位置（经纬度）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "TL主键ID", name = "TeamleaderId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "fse名称", name = "name", required = false, defaultValue = "", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师id", name = "enginnerId", required = false, defaultValue = "", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功" ,response = User.class)
    })
    public ResponseWithData<?> getFseLocation(
            @RequestParam(value="TeamleaderId",required=true) String TeamleaderId,
            @RequestParam(value="name",required=false) String name,
            @RequestParam(value="enginnerId",required=false) String enginnerId){
        try {
            return locationService.getFseLocation(TeamleaderId, name, enginnerId);
        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 获取fse历史定位接口
     * @methodName: getFseLocationLog
     * @description: TODO
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: wlf
     * @date: 2020/4/15 11:05
     **/
    @GetMapping("/getFseLocationLog")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "获取fse历史定位接口", httpMethod = "GET", notes = "获取fse历史定位接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "TL主键ID", name = "teamleaderId", required = false, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师id", name = "fseId", required = true, defaultValue = "", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师名称", name = "fseName", required = false, defaultValue = "", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "开始时间", name = "startTime", required = true, defaultValue = "", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "结束时间", name = "endTime", required = true, defaultValue = "", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> getFseLocationLog(
            @RequestParam(value="teamleaderId",required=false) String teamleaderId,
            @RequestParam(value="fseId",required=true) String fseId,
            @RequestParam(value="fseName",required=false) String fseName,
            @RequestParam(value="startTime",required=true) String startTime,
            @RequestParam(value="endTime",required=true) String endTime){
        try {
            return locationService.getFseLocationLog(teamleaderId, fseId, fseName, startTime, endTime);
        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * App端实时上传经纬度信息接口
     * @methodName: reportLocation
     * @description: TODO
     * @param fseLocationList
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020/3/4 9:47
    **/
    @PostMapping("/reportLocation")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "App端实时上传经纬度信息接口", httpMethod = "POST", notes = "App端实时上传经纬度信息接口")
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功" ,response = FseLocation.class)
    })
    public ResponseWithData<?> reportLocation(@RequestBody FseLocationDto data){
        try {
//            if(StringUtils.isEmpty(fseLocation.getFseId())){
//                return new ResponseWithData<>().error().msg("工程师ID不能为空").code(201);
//            }
//            if(StringUtils.isEmpty(fseLocation.getFseName())){
//                return new ResponseWithData<>().error().msg("工程师姓名不能为空").code(202);
//            }
//            if(fseLocation.getPostx()==null || fseLocation.getPosty()==null){
//                return new ResponseWithData<>().error().msg("经纬度信息不能为空").code(203);
//            }
            return locationService.reportLocation(data);
        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 获取该服务单已派单的FSE列表信息接口
     * @methodName: getSentFseList
     * @description: TODO
     * @param ServiceNoticeId
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020/3/4 18:46
    **/
    @GetMapping("/getSentFseList")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "获取该服务单已派单的FSE列表信息接口", httpMethod = "GET", notes = "获取该服务单已派单的FSE列表信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务单主键ID", name = "ServiceNoticeId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功" ,response = FseLocation.class)
    })
    public ResponseWithData<?> getSentFseList(@RequestParam(value="ServiceNoticeId",required=true) String ServiceNoticeId){
        try {
            return locationService.getSentFseList(ServiceNoticeId);
        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 获取待指派FSE列表信息接口
     * @methodName: getToBeAppointedFse
     * @description: TODO
     * @param ServiceNoticeId
     * @param TeamleaderId
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020/3/5 9:44
    **/
    @GetMapping("/getToBeAppointedFse")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "获取待指派FSE列表信息接口", httpMethod = "GET", notes = "获取待指派FSE列表信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务单主键ID", name = "ServiceNoticeId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "TL主键ID", name = "TeamleaderId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师姓名（搜索条件）", name = "name", required = false, defaultValue = "10009", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功" ,response = FseLocation.class)
    })
    public ResponseWithData<?> getToBeAppointedFse(
            @RequestParam(value="ServiceNoticeId",required=true) String ServiceNoticeId,
            @RequestParam(value="TeamleaderId",required=true) String TeamleaderId,
            @RequestParam(value="name",required=false) String name
    ){
        try {
            return locationService.getToBeAppointedFse(ServiceNoticeId, TeamleaderId, name);
        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 获取TL下所有FSE的实时位置（经纬度）和设备的位置（经纬度）接口
     * @methodName: getLocation
     * @description: TODO
     * @param ServiceNoticeId
     * @param TeamleaderId
     * @return: com.ruoyi.system.api.ResponseWithData<?>
     * @author: shihx
     * @date: 2020/3/6 15:30
    **/
    @GetMapping("/getLocation")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "获取TL下所有FSE的实时位置（经纬度）和设备的位置（经纬度）接口", httpMethod = "GET", notes = "获取TL下所有FSE的实时位置（经纬度）和设备的位置（经纬度）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务单主键ID", name = "ServiceNoticeId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "TL主键ID", name = "TeamleaderId", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功" ,response = FseLocation.class)
    })
    public ResponseWithData<?> getLocation(
            @RequestParam(value="ServiceNoticeId",required=true) String ServiceNoticeId,
            @RequestParam(value="TeamleaderId",required=true) String TeamleaderId
    ){
        try {
            return locationService.getLocation(ServiceNoticeId, TeamleaderId);
        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    @GetMapping("/AddressResolution")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "高德地理解析经纬度接口", httpMethod = "GET", notes = "高德地理解析经纬度接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "地址", name = "address", required = true, defaultValue = "10009", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "城市", name = "city", required = false, defaultValue = "10009", dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功" ,response = FseLocation.class)
    })
    public ResponseWithData<?> AddressResolution(
            @RequestParam(value="address",required=true) String address,
            @RequestParam(value="city",required=false) String city
    ){
        try {
            if(StringUtils.isEmpty(city)){
                city = "";
            }
            Map<String,String> map = locationService.AddressResolution(address, city);
            return new ResponseWithData<>().success(map).msg("查询成功").code(200);
        }catch (Exception e){
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }








}
